package ControlLayer;

import java.util.ArrayList;

import DBLayer.DbConnection;
import DBLayer.DbCustomer;
import DBLayer.IfDbCustomer;
import ModelLayer.Customer;

public class CustomerCtrl
{
	public ArrayList<Customer> findAllCustomers()
    {
		IfDbCustomer dbCust = new DbCustomer();
        ArrayList<Customer> allCusts = new ArrayList<Customer>();
        allCusts = dbCust.getAllCustomers(false);
        return allCusts;
    }
    public Customer findCustomerByName(String name)
    {
    	IfDbCustomer dbCust = new DbCustomer();
        return dbCust.findCustomerByName(name, true);
    }
    public void insertNewCust(String name, String address, String zipCode, String phoneNr, boolean customerGroup)
    {
    	Customer custObj = new Customer();
    	custObj.setName(name);             
    	custObj.setAddress(address);
    	custObj.setZipCode(zipCode);
    	custObj.setPhoneNr(phoneNr);
    	custObj.setCustomerGroup(customerGroup);
 
        try
        {
            DbConnection.startTransaction();
            DbCustomer dbCust = new DbCustomer();
            dbCust.insertCustomer(custObj);
            DbConnection.commitTransaction();
        }
        catch(Exception e)
        {
            DbConnection.rollbackTransaction();
        }
    }

    public int updateCust(String name, String address, String zipCode, String phoneNr, boolean customerGroup)
    {
    	IfDbCustomer dbCust = new DbCustomer();
        Customer cust = new Customer();
        cust.setName(name);
        cust.setAddress(address);
        cust.setZipCode(zipCode);
        cust.setPhoneNr(phoneNr);
        cust.setCustomerGroup(customerGroup);
        return dbCust.updateCustomer(cust);
    }
    
    public int deleteCust(String name)
    {
    	IfDbCustomer dbCust = new DbCustomer();
    	return dbCust.deleteCustomerByName(name);
    }
}
